package com.lcu.bos.web.action;

import com.lcu.bos.modle.Decidedzone;
import com.lcu.bos.modle.PageBean;
import com.lcu.bos.modle.Region;
import com.lcu.bos.service.IDecidedzoneService;
import com.lcu.bos.web.action.base.BaseAction;
import com.lcu.crm.domain.Customer;
import com.lcu.crm.service.CustomerService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class DecidedzoneAction extends BaseAction<Decidedzone> {
    @Autowired
    private IDecidedzoneService decidedzoneService;
    private String [] subareaId;

    public void setSubareaId(String[] subareaId) {
        this.subareaId = subareaId;
    }

    public String save() {
        System.out.println(getModel().getStaff());
        System.out.println(getModel());
        Decidedzone dz = getModel();
        decidedzoneService.save(dz,subareaId);
        return null;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String update() throws UnsupportedEncodingException {
        return null;
    }

    @Override
    public String get() {
        return null;
    }

    @Override
    public String findAll() {
        return null;
    }
    private int page;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    private int rows;

    public void pageQuery() throws IOException {
        //1.接收参数 page当前页 rows【每页显示多少条数据】
        PageBean<Decidedzone> pb = new PageBean<>();
        //2.调用service
        //封装条件
        pb.setCurrentPage(page);
        pb.setPageSize(rows);
        DetachedCriteria cd = DetachedCriteria.forClass(Decidedzone.class);//离线查询条件
        pb.setDetachedCriteria(cd);
        decidedzoneService.pageQuery(pb);
        //3.返回json格式数据
        JsonConfig config = new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"currentPage", "pageSize", "detachedCriteria"});//排出那些属性不用转成json数据
        JSONObject jb = JSONObject.fromObject(pb, config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type", "text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    //======远程调用  获取已关联的客户信息
    @Autowired
    private CustomerService customerService;
    public void findhasassociationCustomers() throws IOException {
        List<Customer> list = customerService.findhasassociationCustomers(getModel().getId());
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        //config.setExcludes(new String[]{"decidedzone","region"});
        JSONArray jb=JSONArray.fromObject(list,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    //======远程调用  获取未关联的客户信息

    public void findnoassociationCustomers() throws IOException {
        List<Customer> list = customerService.findnoassociationCustomers();
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        //config.setExcludes(new String[]{"decidedzone","region"});
        JSONArray jb=JSONArray.fromObject(list,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    /*关联客户到定区*/
    private Integer[] customerIds;

    public void setCustomerIds(Integer[] customerIds) {
        this.customerIds = customerIds;
    }

    public String assigncustomerstodecidedzone(){
        //1.一个是客户的id，一个是定区id

        //2.远程调用
        customerService.assignCustomersToDecidedZone(customerIds,getModel().getId());
        return SUCCESS;
    }
}
